*****************************
*** TREATMENT ASSIGNMENTS ***
*****************************

	*** Set date 
		set seed 120993 
		local month = "10"
		local day = "15"
		local deliverymonth = "10"
		local deliverydate = "17" 
	
	*** Load exploration sampling assignments
		import delimited "$root/DATA/Adaptive_Trial_Output/Treatment_assignments/2020-`month'-`day'_t_assignments.csv", clear
		gen n = 1
		collapse (sum) n, by(treatment)
		egen total = total(n)
		gen rate = n/total
		keep treatment rate
		save "$output/2020-`month'-`day'_t_rates.dta", replace

		use "$output/2020-`month'-`day'_t_rates.dta", clear
		levelsof treatment, local(treatments)
		local ratelist = ""
		foreach arm of local treatments {
			summ rate if treatment == `arm'
			local ratelist = "`ratelist'" + " `r(mean)'"
		}
		di "`ratelist'"
	
	*** Assign treatments to next round 
		use "$root/DATA/Saran_Admin_Data/bihar_actualsample_trial_identified_complete_25Sept20.dta", clear
		keep if dateofdelivery == mdy(`deliverymonth', `deliverydate', 2020)
		replace dateofdelivery = mdy(10,17,2020) if dateofdelivery == mdy(10,16,2020)
		sort phonenumber, stable
		sort stratum pairnumber
		by stratum pairnumber: gen n = _n 
		stratarand if n == 1, gentreat(arm) strata(stratum) group_percentages(`ratelist') replace exact
		sort stratum pairnumber arm
		by stratum pairnumber: replace arm = arm[_n-1] if !missing(arm[_n-1])
		replace arm = runiformint(1,10) if missing(arm)
		preserve 
			keep scrambledid arm
			rename arm treatment 
			assert !missing(treatment)
			label values treatment arm 
			if "`deliverymonth'" == "08" & "`deliverydate'" == "31" {
				local deliverymonth = "09"
				local deliverydate = "01"
			}
			export delimited "$output/sms_assignments_`deliverymonth'`deliverydate'20.csv", replace 
			save "$root/DATA/Adaptive_Trial_Output/SMS_assignments/sms_assignments_`deliverymonth'`deliverydate'20.dta", replace
		restore 

	*** Create output for telerivet messages
		replace freq = "Twice in the morning (7-8 am & 10-11 am) for 2 days"                   if ( arm>=1 & arm <= 5 )
		replace freq = "Once in the morning (7-8 am) & once in the evening(6-7 pm) for 2 days" if ( arm>=6 & arm <= 10 )	

		replace frame = "Neutral"        if inlist( arm, 1, 6 )
		replace frame = "Public gain"    if inlist( arm, 2, 7 )
		replace frame = "Public loss"    if inlist( arm, 3, 8 )
		replace frame = "Private gain"   if inlist( arm, 4, 9 )
		replace frame = "Private loss"   if inlist( arm, 5, 10)
		
		la var freq  "Frequency"
		la var frame "Message frame"
	
	* Need 4 rows per number, one for each SMS sent at the different days/times
		gen id = _n
		expand 4
		
		bysort id: gen messageround = _n
		drop id
		assert inlist(messageround, 1, 2, 3, 4)
		la var messageround "Message round"
	
		decode behavior, gen(behavior_string)
	
	
	* First 2 of the 4 messages are on on "dateofdelivery"
	* Remaining 2 messages are a day later, ie, dateofdelivery + 1
	
	
	* Time of delivery depends on "frequency"
		
	generate double smsdatetime = .
	
	* Twice in the morning
	replace smsdatetime = dhms(dateofdelivery, 7,  30, 0)   if messageround==1 & freq== "Twice in the morning (7-8 am & 10-11 am) for 2 days"
	replace smsdatetime = dhms(dateofdelivery, 10, 30, 0)   if messageround==2 & freq== "Twice in the morning (7-8 am & 10-11 am) for 2 days"
																				  
	replace smsdatetime = dhms(dateofdelivery+1, 7,  30, 0) if messageround==3 & freq== "Twice in the morning (7-8 am & 10-11 am) for 2 days"
	replace smsdatetime = dhms(dateofdelivery+1, 10, 30, 0) if messageround==4 & freq== "Twice in the morning (7-8 am & 10-11 am) for 2 days"
																				  
	* One morning, one evening                                                    
	replace smsdatetime = dhms(dateofdelivery, 7,  30, 0)   if messageround==1 & freq== "Once in the morning (7-8 am) & once in the evening(6-7 pm) for 2 days"
	replace smsdatetime = dhms(dateofdelivery, 18, 30, 0)   if messageround==2 & freq== "Once in the morning (7-8 am) & once in the evening(6-7 pm) for 2 days"
																				  
	replace smsdatetime = dhms(dateofdelivery+1, 7,  30, 0) if messageround==3 & freq== "Once in the morning (7-8 am) & once in the evening(6-7 pm) for 2 days"
	replace smsdatetime = dhms(dateofdelivery+1, 18, 30, 0) if messageround==4 & freq== "Once in the morning (7-8 am) & once in the evening(6-7 pm) for 2 days"

	la var smsdatetime "Date-time for SMS"
	format smsdatetime  %tcDD/NN/CCYY_HH:MMAM


	* Content of message 
	
	gen smstext_english = ""
	gen smstext_hindi    = ""
		
	la var smstext_english "Text for SMS - English"
	la var smstext_hindi   "Text for SMS - Hindi"

	
	* Distancing
	replace smstext_english = "Coronavirus is here. Outside the house, keep a distance of at least two arms from others." if behavior_string == "Distancing" & frame=="Neutral"
	replace smstext_hindi   = "कोरोनावायरस यहाँ है। घर के बाहर, दूसरों से कम से कम दो हाथ की दूरी बनाए रखें।" if behavior_string == "Distancing" & frame=="Neutral"

	replace smstext_english = "Save lives. Your action can protect your family from coronavirus. Outside the house, keep a distance of at least two arms from others." if behavior_string == "Distancing" & frame=="Private gain"
	replace smstext_hindi   = "जीवन बचाए। आपकी सावधानी आपके परिवार को कोरोनावायरस से बचा सकते है। घर के बाहर, दूसरों से कम से कम दो हाथ की दूरी बनाए रखें।" if behavior_string == "Distancing" & frame=="Private gain"

	replace smstext_english = "Save lives. Your action can protect our community from coronavirus. Outside the house, keep a distance of at least two arms from others." if behavior_string == "Distancing" & frame=="Public gain"
	replace smstext_hindi   = "जीवन बचाए। आपकी सावधानी हमारे समुदाय को कोरोनावायरस से बचा सकते है। घर के बाहर, दूसरों से कम से कम दो हाथ की दूरी बनाए रखें।" if behavior_string == "Distancing" & frame=="Public gain"

	replace smstext_english = "Coronavirus kills. Your action can put your family at risk of infection. Outside the house, keep a distance of at least two arms from others." if behavior_string == "Distancing" & frame=="Private loss"
	replace smstext_hindi   = "कोरोनावायरस मारता है। आपकी असावधानी आपके परिवार को कोरोनावायरस के खतरे में डाल सकती है। घर के बाहर, दूसरों से कम से कम दो हाथ की दूरी बनाए रखें।" if behavior_string == "Distancing" & frame=="Private loss"
	
	replace smstext_english = "Coronavirus kills. Your action can put our community at risk of infection. Outside the house, keep a distance of at least two arms from others." if behavior_string == "Distancing" & frame=="Public loss"
	replace smstext_hindi   = "कोरोनावायरस मारता है। आपकी असावधानी हमारे समुदाय को कोरोनावायरस के खतरे में डाल सकती है। घर के बाहर, दूसरों से कम से कम दो हाथ की दूरी बनाए रखें।" if behavior_string == "Distancing" & frame=="Public loss"

	* Hand washing
	replace smstext_english = "Coronavirus is here. Before touching any food or touching your face, wash your hands with water and soap." if behavior_string == "Handwashing" & frame=="Neutral"
	replace smstext_hindi   = "कोरोनावायरस यहाँ है। किसी भी खाने को छूने या अपने चेहरे को छूने से पहले, अपने हाथों को पानी और साबुन से धो लें।" if behavior_string == "Handwashing" & frame=="Neutral"

	replace smstext_english = "Save lives. Your action can protect your family from coronavirus. Before touching any food or touching your face, wash your hands with water and soap." if behavior_string == "Handwashing" & frame=="Private gain"
	replace smstext_hindi   = "जीवन बचाए। आपकी सावधानी आपके परिवार को कोरोनावायरस से बचा सकते है। किसी भी खाने को छूने या अपने चेहरे को छूने से पहले, अपने हाथों को पानी और साबुन से धो लें।" if behavior_string == "Handwashing" & frame=="Private gain"

	replace smstext_english = "Save lives. Your action can protect our community from coronavirus. Before touching any food or touching your face, wash your hands with water and soap." if behavior_string == "Handwashing" & frame=="Public gain"
	replace smstext_hindi   = "जीवन बचाए। आपकी सावधानी हमारे समुदाय को कोरोनावायरस से बचा सकते है। किसी भी खाने को छूने या अपने चेहरे को छूने से पहले, अपने हाथों को पानी और साबुन से धो लें।" if behavior_string == "Handwashing" & frame=="Public gain"

	replace smstext_english = "Coronavirus kills. Your action can put your family at risk of infection. Before touching any food or touching your face, wash your hands with water and soap." if behavior_string == "Handwashing" & frame=="Private loss"
	replace smstext_hindi   = "कोरोनावायरस मारता है। आपकी असावधानी आपके परिवार को कोरोनावायरस के खतरे में डाल सकती है। किसी भी खाने को छूने या अपने चेहरे को छूने से पहले, अपने हाथों को पानी और साबुन से धो लें।" if behavior_string == "Handwashing" & frame=="Private loss"
	
	replace smstext_english = "Coronavirus kills. Your action can put our community at risk of infection. Before touching any food or touching your face, wash your hands with water and soap." if behavior_string == "Handwashing" & frame=="Public loss"
	replace smstext_hindi   = "कोरोनावायरस मारता है। आपकी असावधानी हमारे समुदाय को कोरोनावायरस के खतरे में डाल सकती है। किसी भी खाने को छूने या अपने चेहरे को छूने से पहले, अपने हाथों को पानी और साबुन से धो लें।" if behavior_string == "Handwashing" & frame=="Public loss"

	drop behavior_string

	assert !mi(smstext_english)==!mi(smstext_hindi)
	assert !mi(smstext_english)==!mi(smsdatetime)
	
	compress
	
	
		keep if !mi(smsdatetime)

		order scrambledid  phonenumber statename block smsdatetime smstext*  centertype  idate behavior frame freq
		keep  scrambledid  phonenumber statename block smsdatetime smstext*  centertype  idate behavior frame freq

		sort smsdatetime smstext* behavior

		notes: List for Telerivet / sending SMS
		notes: Each number appears 4 times - once for every SMS to be sent

		la data "Intervention - phone numbers and treatment"
		save "bihar_list_for_Telerivet.dta", replace
		
		export excel using "$output/Telerivet_`deliverymonth'`deliverydate'20.xlsx", firstrow(variables) replace
		